﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TTIServer;
using Treblotto_TabletDefs;

namespace TTIServer.TTISettings
{
	/// <summary>
	/// Interaction logic for EditPresets.xaml
	/// </summary>
	public partial class EditPresets : Window
	{
		KeyboardHook myKeyboardHook;
		List<KeyboardHook.VKeys> PressedKeys;
		Boolean HookInstalled = false;
		TabletSettings TabetSettingsRef;

		public EditPresets( TabletSettings inTabletSettingsRef, Treblotto_TabletDefs.ExpressKeyVals.WhichSide ws, Treblotto_TabletDefs.ExpressKeyVals.ExpressKeys ek )
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
			myKeyboardHook = new KeyboardHook();
			myKeyboardHook.Install();
			HookInstalled = true;



			PressedKeys = new List<KeyboardHook.VKeys>(0);

			myKeyboardHook.KeyDown += this.HookKeyDown;
			myKeyboardHook.KeyUp += myKeyboardHook_KeyUp;
			
			
		}

		void myKeyboardHook_KeyUp(KeyboardHook.VKeys key)
		{
			PressedKeys.Remove(key);
			UpdateKeyBox();
		}

		private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.DragMove();
		}

		private void button1_Click(object sender, RoutedEventArgs e) //okay button
		{
			if (HookInstalled)
				myKeyboardHook.Uninstall();
			//Update Settings
			this.Close();
		}

		private void button2_Click(object sender, RoutedEventArgs e)
		{
			myKeyboardHook.Uninstall();
			this.Close();
		}

		private void button3_Click(object sender, RoutedEventArgs e)
		{
			myKeyboardHook.Uninstall();
			//Restore Back to default Settings
			this.Close();
		}

		private void button4_Click(object sender, RoutedEventArgs e)
		{
			PressedKeys.Clear();
			UpdateKeyBox();
		}

		private void button5_Click(object sender, RoutedEventArgs e)
		{

		}

		private void HookKeyDown ( KeyboardHook.VKeys myKey)
		{

			if (!PressedKeys.Contains(myKey))
				PressedKeys.Add(myKey);
			UpdateKeyBox();

			if ( (myKey >= (KeyboardHook.VKeys)0x41) && (myKey <= (KeyboardHook.VKeys)0x5A))
			{
				myKeyboardHook.Uninstall();
				HookInstalled = false;
			}

		}

		private string RemoveKeyFromKey (KeyboardHook.VKeys inKey)
		{
			string tString = inKey.ToString();

			if (tString.Contains("KEY_"))
			{
				int location = tString.IndexOf("KEY_");
				tString = tString.Substring(location+4);

			}

			if (tString.Contains("CONTROL"))
				tString = "CTRL";

			if (tString.Contains("MENU"))
				tString = "ALT";

			if (tString.Contains("SHIFT"))
				tString = "SHIFT";

			return tString;
		}

		private void UpdateKeyBox()
		{
			String displayString = "";

			foreach (KeyboardHook.VKeys vkPressed in PressedKeys)
			{
				if (displayString.Length == 0)
					displayString = RemoveKeyFromKey(vkPressed);
				else
					displayString += " + " + RemoveKeyFromKey(vkPressed);

			}

			textBox.Text = displayString;

		}

	}
}
